package com.jiangjiang.eshop.order.price.v1.calculator.impl;

import com.alibaba.fastjson.JSONObject;
import com.jiangjiang.eshop.order.dto.CouponDTO;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import com.jiangjiang.eshop.order.price.v1.calculator.CouponCalculator;
import org.springframework.stereotype.Component;

/**
 * @Author Jianghaowen
 * @Date 2021/7/14
 * @Description: 现金券抵扣金额计算组件
 * @Version V1.0
 **/
@Component
public class CashCouponCalculator implements CouponCalculator {

    /**
     *现金券抵扣金额计算组件
     * @param infoDTO 订单信息
     * @param couponDTO 优惠券信息
     * @return
     * @throws Exception
     */
    @Override
    public Double calculate(OrderInfoDTO infoDTO, CouponDTO couponDTO) throws Exception {
        Double payableAmount = infoDTO.getPayableAmount();
        JSONObject jsonObject = JSONObject.parseObject(couponDTO.getRule());
        Double discountAmount = jsonObject.getDouble("discountAmount");

        if(discountAmount > payableAmount){
            return payableAmount;
        }
        return discountAmount;
    }
}
